package com.tech.middle_ware.h2;

import lombok.extern.slf4j.Slf4j;
import org.h2.tools.Server;

import java.io.File;

@Slf4j
public class H2Server1 {
    public static int NO = 1;
    public static String PROJECT_PATH = System.getProperty("user.dir");
    public static String PROJECT_RESOURCE_PATH = PROJECT_PATH+"/AAresources";
    public static String H2_BASE_DIR = PROJECT_RESOURCE_PATH +"/db/h2/server"+NO;

    public static void main(String[] args){
        File file = new File(H2_BASE_DIR);
        if(!file.exists()){
            file.mkdirs();
        }
        H2Server1 demo = new H2Server1();
        try {
            demo.startTcpServer();
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /**
     * 数据库服务
     * jdbc:h2:tcp://localhost:9092/./h2
     *  默认端口：9092
     *  默认数据库：h2
     */
    public void startTcpServer() throws Exception {
        Server server = Server.createTcpServer(
                "-tcp",
                "-tcpAllowOthers",
                "-tcpPort", "909"+NO,
                // 使用守护线程
                "-tcpDaemon",
                "-ifNotExists",
                "-baseDir", H2_BASE_DIR
        ).start();
        Thread.currentThread().join();// 阻塞线程；测试完成时会关闭h2
//		server.stop();
    }
}

